package br.com.empresa.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.GridLayout;

import javax.swing.ComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.Color;

import javax.swing.SwingConstants;

import br.com.empresa.modelo.Animal;
import br.com.empresa.modelo.Cliente;
import br.com.persistencia.AnimalDao;
import br.com.persistencia.ClienteDao;

import java.awt.Font;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class AnimalGUI extends JFrame {

	private JPanel contentPane;
	private JTextField txNomeAnimal;
	private JTextField txCodigo;
	private JTextField txRaca;
	private JTextField txSexo;
	private JTextField txDataNascimento;
	private JTextField txProprietario;
	private JComboBox<Cliente> cbxCliente;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AnimalGUI frame = new AnimalGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AnimalGUI() {
		setTitle("PetShop C\u00E3o & Gatos");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 464, 310);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new GridLayout(0, 1, 0, 0));
		
		JPanel panel = new JPanel();
		contentPane.add(panel);
		panel.setLayout(null);
		
		txNomeAnimal = new JTextField();
		txNomeAnimal.setColumns(10);
		txNomeAnimal.setBounds(110, 90, 228, 20);
		panel.add(txNomeAnimal);
		
		JLabel lblCodigo = new JLabel("C\u00F3digo");
		lblCodigo.setBounds(20, 62, 46, 14);
		panel.add(lblCodigo);
		
		txCodigo = new JTextField();
		txCodigo.setEditable(false);
		txCodigo.setColumns(10);
		txCodigo.setBounds(110, 59, 53, 20);
		panel.add(txCodigo);
		
		JLabel lblNomeAnimal = new JLabel("Nome animal :");
		lblNomeAnimal.setBounds(20, 93, 88, 14);
		panel.add(lblNomeAnimal);
		
		JLabel lblRaca = new JLabel("Ra\u00E7a:");
		lblRaca.setBounds(20, 118, 70, 14);
		panel.add(lblRaca);
		
		JLabel lblSexo = new JLabel("Sexo :");
		lblSexo.setBounds(20, 146, 70, 14);
		panel.add(lblSexo);
		
		JLabel lblDataNascimento = new JLabel("Data Nascimento:");
		lblDataNascimento.setBounds(20, 171, 89, 14);
		panel.add(lblDataNascimento);
		
		txRaca = new JTextField();
		txRaca.setColumns(10);
		txRaca.setBounds(110, 115, 228, 20);
		panel.add(txRaca);
		
		txSexo = new JTextField();
		txSexo.setColumns(10);
		txSexo.setBounds(110, 143, 228, 20);
		panel.add(txSexo);
		
		txDataNascimento = new JTextField();
		txDataNascimento.setColumns(10);
		txDataNascimento.setBounds(110, 168, 228, 20);
		panel.add(txDataNascimento);
		
		JLabel lblProprietario = new JLabel("Proprietario :");
		lblProprietario.setBounds(20, 199, 88, 14);
		panel.add(lblProprietario);
		
//		txProprietario = new JTextField();
//		txProprietario.setColumns(10);
//		txProprietario.setBounds(110, 196, 228, 20);
//		panel.add(txProprietario);

		cbxCliente = new JComboBox<Cliente>();
		
		cbxCliente.setModel((ComboBoxModel<Cliente>)(new ClienteDao()).listar());
		cbxCliente.setBounds(110, 196, 228, 20);
		panel.add(cbxCliente);
		
		
		JButton btnSalvar = new JButton("Salvar");
		btnSalvar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			Animal a = new Animal();
			a.setNome(txNomeAnimal.getText());
			a.setRaca(txRaca.getText());
			a.setSexo(txSexo.getText());
			a.setDataNascimento(formataData(txDataNascimento.getText()));
			a.setCliente((Cliente)cbxCliente.getSelectedItem());
			AnimalDao dao = new AnimalDao();
			dao.inserir(a);
			}
		});
		btnSalvar.setBounds(240, 228, 89, 23);
		panel.add(btnSalvar);
				
				
		JButton btnLimpar = new JButton("Limpar");
		btnLimpar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});
		btnLimpar.setBounds(250, 227, 89, 23);
		panel.add(btnLimpar);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBackground(new Color(173, 216, 230));
		panel_1.setBounds(0, 0, 438, 51);
		panel.add(panel_1);
		panel_1.setLayout(null);
		
		JLabel lblCadastroDeAnimal = new JLabel("Cadastro de Animal");
		lblCadastroDeAnimal.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		lblCadastroDeAnimal.setBounds(135, 5, 130, 35);
		lblCadastroDeAnimal.setHorizontalAlignment(SwingConstants.CENTER);
		lblCadastroDeAnimal.setForeground(Color.WHITE);
		panel_1.add(lblCadastroDeAnimal);
		
		btnLimpar.setBounds(339, 228, 89, 23);
		panel.add(btnLimpar);
	}
	
	public static java.sql.Date formataData(String data) {
		java.sql.Date retorno = null;
		String dataFormatada = data.substring(6, 10) + "-" + data.substring(3, 5) + "-" + data.substring(0,2);
		retorno = java.sql.Date.valueOf(dataFormatada);
		return retorno;
	}
}
